package com.sell.controller;

import com.sell.common.HttpResult;
import com.sell.converter.OrderFrom2OrderDTOConverter;
import com.sell.dto.OrderDTO;
import com.sell.exceptions.ResultEnum;
import com.sell.exceptions.SellException;
import com.sell.from.OrderFrom;
import com.sell.service.BuyerService;
import com.sell.service.OrderService;
import com.sell.utils.JsonUtil;
import com.sell.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: megan
 * @eMail: meganmail@vip.qq.com
 * @date: 2018/12/6 15:29
 * @version: v1.0
 * @description: 买家订单controller
 */
@RestController
@RequestMapping("/buyer/order/")
@Slf4j
public class BuyerOrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private BuyerService buyerService;

    /**
     * 创建订单
     *
     * @param orderFrom     from
     * @param bindingResult 校验
     * @return Map
     */
    @PostMapping("/create")
    public ResultVo<Map<String, Object>> create(@Valid OrderFrom orderFrom, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            log.error("[创建订单]参数不正确，orderFrom：{}", JsonUtil.obj2StringPretty(orderFrom));
            throw new SellException(ResultEnum.PARAM_ERROR.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        OrderDTO orderDTO = OrderFrom2OrderDTOConverter.converter(orderFrom);
        if (orderDTO == null || CollectionUtils.isEmpty(orderDTO.getOrderDetailList())) {
            log.error("【创建订单】购物车不能为空");
            throw new SellException(ResultEnum.CART_EMPTY);
        }
        OrderDTO createResult = orderService.create(orderDTO);
        Map<String, String> resultMap = new HashMap<String, String>();
        resultMap.put("orderId", createResult.getOrderId());
        return HttpResult.success(resultMap);
    }

    /**
     * 订单列表
     *
     * @param openid 用户的openid
     * @param page   第几页
     * @param size   一页多少条数据
     * @return List<OrderDTO>
     */
    @GetMapping("/list")
    public ResultVo<List<OrderDTO>> list(@RequestParam(value = "openid") String openid,
                                         @RequestParam(value = "page", defaultValue = "0", required = false) Integer page,
                                         @RequestParam(value = "size", defaultValue = "10", required = false) Integer size) {
        if (StringUtils.isBlank(openid)) {
            log.error("【查询订单列表】openid不能为空");
            throw new SellException(ResultEnum.PARAM_ERROR);
        }
        Page<OrderDTO> orderDTOPage = orderService.findList(openid, new PageRequest(page, size));
        return HttpResult.success(orderDTOPage.getContent());
    }

    /**
     * 订单详情
     *
     * @param openid  用户的openid
     * @param orderId 订单编号
     * @return OrderDTO
     */
    @GetMapping("/detail")
    public ResultVo<OrderDTO> detail(@RequestParam(value = "openid") String openid,
                                     @RequestParam(value = "orderId") String orderId) {
        return HttpResult.success(buyerService.findOrderOne(openid, orderId));
    }

    /**
     * 取消订单
     *
     * @param openid  用户的openid
     * @param orderId 订单编号
     * @return OrderDTO
     */
    @PostMapping("/cancel")
    public ResultVo cancel(@RequestParam(value = "openid") String openid,
                           @RequestParam(value = "orderId") String orderId) {
        OrderDTO orderDTO = orderService.findOne(orderId);
        buyerService.cancelOrder(orderDTO, openid);
        return HttpResult.success();
    }
}
